package TestSparkConversionOperator.KeyValuePairRDD;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.PairFunction;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;


public class SparkMapTOPair {
    /**
     * 用转换算子mapToPair创建键值对RDD
     * @param args
     */
    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf().setAppName("SparkMapTOPair").setMaster("local");
        JavaSparkContext sc = new JavaSparkContext(sparkConf);
        List<String> data = Arrays.asList("2021-10,47", "2020-10,47");
        JavaRDD<String> rdd1 = sc.parallelize(data);

        JavaPairRDD<String, String> pairRDD = rdd1.mapToPair(new PairFunction<String, String, String>() {
            @Override
            public Tuple2<String, String> call(String s) throws Exception {

                return new Tuple2<>(s.split(",")[0], s);
            }
        });

        System.out.println(pairRDD.collect());

    }



}
